/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.mbfasttrack.service;

import java.util.ArrayList;
import java.util.Date;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
public class UserServiceSecurity extends Base implements UserDetailsService {

    /**
     * 
     * @param username
     * @return
     * @throws UsernameNotFoundException
     * @throws DataAccessException
     */
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DataAccessException {

        com.mbfasttrack.model.User subscriber = userRepository.findOneByUsername(username);

        System.err.println("username: " + username);

        if (subscriber == null) {
            System.err.print("subscriber == null ");
            return null;
        }

        System.err.println("subscriber: " + subscriber);

        ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        GrantedAuthority supervisor = new GrantedAuthorityImpl("ROLE_USER");
        authorities.add(supervisor);

        User u = new User(subscriber.getUsername(), subscriber.getPassword(),/*subscriber.isActive()*/ true, true, true, true, authorities);

        subscriber.setLastLogin(new Date(System.currentTimeMillis()));
        userRepository.save(subscriber);

        System.err.println("subscriber: " + subscriber);

        return u;



    }

    public UserDetails loadUserByEmail(String email)
            throws UsernameNotFoundException, DataAccessException {

        com.mbfasttrack.model.User subscriber = userRepository.findOneByEmail(email);

        System.err.print("email: " + email);

        if (subscriber == null) {
            return null;
        }

        ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        GrantedAuthority supervisor = new GrantedAuthorityImpl("ROLE_USER");
        authorities.add(supervisor);

        User u = new User(subscriber.getUsername(), subscriber.getPassword(),/*subscriber.isActive()*/ true, true, true, true, authorities);

        subscriber.setLastLogin(new Date(System.currentTimeMillis()));
        mongoTemplate.save(subscriber);

        return u;



    }
}
